package com.servlet;

import java.io.IOException;

import com.javabean.Student;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				//设置编码格式
				req.setCharacterEncoding("utf-8");
				resp.setContentType("text/html;charset=utf-8");
				//获取参数
				String no = req.getParameter("no");
				String name = req.getParameter("name");
				String age = req.getParameter("age");
				String sex = req.getParameter("sex");
				//生成对象
				Student st = new Student();
				//给对象赋值
				st.setNo(no);
				st.setName(name);
				st.setAge(Integer.parseInt(age));
				st.setSex(sex);
				//将st添加到ArrayList中
				Student.add(st);
				//跳转到stu_list.jsp页面
				req.getRequestDispatcher("stu_list.jsp").forward(req, resp);
	}	
}
